<template>
  <div class="tabs-app">
    <!-- 搜索表单 -->
    <el-form :inline="true" class="search-component">
      <el-form-item label="日志内容">
        <el-input v-model="searchCondition.keyWord" placeholder="请输入日志内容" clearable />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="searchCondition.createTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="medium" @click="initLogTable">搜索</el-button>
      <el-button type="warning" icon="el-icon-refresh" size="medium" @click="resetSearchCondition">重置</el-button>
    </el-form>
    <!-- 操作按钮 -->
    <el-form :inline="true" class="operation-component">
      <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteLog">批量删除</el-button>
      <el-button type="success" icon="el-icon-download" size="mini" @click="exportLog">批量导出</el-button>
    </el-form>
    <!-- 列表 -->
    <el-table
      ref="logTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="expand">
        <template slot-scope="{row}">
          <el-form label-position="left" class="log-table-expand">
            <el-form-item label="请求参数">
              <span>{{ row.params }}</span>
            </el-form-item>
            <el-form-item label="请求方法">
              <span>{{ row.method }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" />
      <el-table-column prop="username" label="操作人" width="width" />
      <el-table-column prop="requestIp" label="IP" width="width" />
      <el-table-column prop="address" label="IP来源" width="width" />
      <el-table-column prop="description" label="操作描述" width="width" />
      <el-table-column prop="browser" label="浏览器" width="width" />
      <el-table-column prop="time" label="请求耗时" width="width">
        <template slot-scope="{ row }">
          <el-tag v-if="row.time <= 1000" type="success">{{ row.time }}ms</el-tag>
          <el-tag v-else-if="row.time > 1000 && row.time <= 2000" type="warning">{{ row.time }}ms</el-tag>
          <el-tag v-else type="danger">{{ row.time }}ms</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="发生时间" width="width" sortable />
      <el-table-column label="异常详情" width="width" align="center">
        <template slot-scope="{row}">
          <el-button size="mini" type="text" @click="showDetail(row.id)">查看详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteLog(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page.current"
      :total="page.total"
      :page-size="page.size"
      :page-sizes="[10, 20, 50, 100]"
      style="padding: 20px 0"
      layout="total,prev, pager, next, sizes,jumper"
      @current-change="initLogTable"
      @size-change="handleSizeChange"
    />
    <el-dialog title="异常详情" :visible.sync="dialogVisible" append-to-body top="30px" width="66%">
      <pre>{{ errorDetail }}</pre>
    </el-dialog>
  </div>
</template>

<script>
import Config from '@/settings'
export default {
  name: 'AdminErrorLog',
  data() {
    return {
      page: {
        total: 0, // 总记录数
        current: 1, // 当前页码
        size: 10, // 每页记录数
        orders: [{ column: 'id', asc: true }]
      },
      searchCondition: {
        createTime: [],
        keyWord: '',
        logType: 'ERROR'
      },
      tableData: [], // 列表数据
      errorDetail: '',
      dialogVisible: false
    }
  },
  mounted() {
    this.initLogTable()
  },
  methods: {
    async initLogTable(current = 1) {
      const { page, searchCondition } = this
      if (Number.isInteger(current)) {
        page.current = current
      }
      const result = await this.$API.adminLog.pageQuery({ searchCondition, page })
      if (result.code === 200) {
        this.tableData = result.data.records
        this.page.current = result.data.current
        this.page.size = result.data.size
        this.page.total = result.data.total
      }
    },
    resetSearchCondition() {
      this.searchCondition.createTime = []
      this.searchCondition.keyWord = ''
      this.initLogTable()
    },
    async deleteLog(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const result = await this.$API.adminLog.deleteLog(row.logId)
          if (result.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              showClose: true,
              duration: Config.duration
            })
            this.initLogTable()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /*
      列表选中状态发生改变的监听回调
    */
    handleSelectionChange(selection) {
      this.selectedIds = selection.map((item) => item.logId)
    },
    /*
    处理pageSize发生改变的监听回调
    */
    handleSizeChange(pageSize) {
      this.page.size = pageSize
      this.initLogTable()
    },
    async exportLog() {
      await this.$API.adminLog.exportLog()
    },
    async showDetail(id) {
      const result = await this.$API.adminLog.showDetail(id)
      if (result.code === 200) {
        this.errorDetail = result.data.exception
        this.dialogVisible = true
      }
    }
  }
}
</script>

<style scoped>
.log-table-expand {
  font-size: 0;
}

.log-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.log-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
